package com.cote.douhahou.pretloan.level;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;


public class CircleAkomapalendBarView extends View {

    private Paint bgPaint,progressPaint;

    private int bgColor;
    private int progressColor1;
    private int progressColor2;

    private int maxProgress;
    private int progress;

    public CircleAkomapalendBarView(Context context, AttributeSet attrs){
        super(context,attrs);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){

        @SuppressLint("CustomViewStyleable") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleBarView);

        bgColor = typedArray.getColor(R.styleable.CircleBarView_bg_color,context.getColor(R.color.color_vip_bg_1));
        progressColor1 = typedArray.getColor(R.styleable.CircleBarView_progress_color1,context.getColor(R.color.color_vip_progress_start_1));
        progressColor2 = typedArray.getColor(R.styleable.CircleBarView_progress_color2,context.getColor(R.color.color_vip_1));
        maxProgress = typedArray.getInt(R.styleable.CircleBarView_max_progress,100);
        progress = typedArray.getInt(R.styleable.CircleBarView_progress,0);

        typedArray.recycle();

        bgPaint = new Paint();
        bgPaint.setColor(bgColor);
        bgPaint.setAntiAlias(true);
        bgPaint.setStrokeCap(Paint.Cap.ROUND);
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeWidth(AkomapaLendUtil.sizeDp2Px(context,5));

        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(AkomapaLendUtil.sizeDp2Px(context,5));

    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float paddingH = AkomapaLendUtil.sizeDp2Px(getContext(),30);
        float paddingB = AkomapaLendUtil.sizeDp2Px(getContext(),5);

        float r = getWidth()*2 - paddingH;
        float c = 30f;

        float left = (getWidth()/2f) - r;
        float right = getWidth() + Math.abs(left);
        float top = getHeight() - 2*r;
        float bottom = 2*r + top - paddingB;

        canvas.drawArc(
                left,top,right,bottom,
                90f-c/2f,c,false,bgPaint
        );

        float sweepAngle = c / (float) maxProgress * (float) progress;
        float startAngle2 = (90+c/2f) - sweepAngle;

        progressPaint.setShader(new LinearGradient(0,0,getWidth()/(float)maxProgress*progress,0,progressColor1,progressColor2, Shader.TileMode.CLAMP));

        canvas.drawArc(
                left,top,right,bottom,
                startAngle2,sweepAngle,false,progressPaint
        );

    }

    public void refresh(int maxProgress, int progress, int bgColor, int progressColor1, int progressColor2){

        if (maxProgress<0) maxProgress = 0;
        if (progress > maxProgress) progress = maxProgress;

        this.progress = progress;
        this.maxProgress = maxProgress;
        this.bgColor = bgColor;
        this.progressColor1 = progressColor1;
        this.progressColor2 = progressColor2;

        bgPaint.setColor(bgColor);

        postInvalidate();

    }

}